﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ARR.Entities;
using ARR.DataModel.Repositories;
using DDF.Entity;
using System.Linq.Expressions;
using ARR.Entities.Gateways;
using System.Transactions;
using System.ServiceModel;
using ARR.Shared;

namespace ARR.Services.Domain
{
    public static class CountriesDomain
    {
        public static List<Country> GetAll()
        {
            var rows = CountriesRepository.Select();
            var countries = rows.ConvertAll<Country>(c => new Country(c));

            countries.ForEach(c => c.ChangeState(EntityState.Original));

            return countries;
        }

        public static Country GetById(int countryID)
        {
            var row = CountriesRepository.Select(c => c.Id == countryID).SingleOrDefault();

            Country country = new Country(row);
            country.ChangeState(EntityState.Original);
            
            return country;
        }

        public static Country GetByCode(string countryCode)
        {
            var row = CountriesRepository.Select(c => c.Code == countryCode).SingleOrDefault();

            Country country = new Country(row);
            country.ChangeState(EntityState.Original);

            return country;
        }

        public static void SaveCountries(List<Country> countries)
        {
            using (TransactionScope scope = new TransactionScope())
            {
                List<Country> modifieds = countries.Where(t => t.GetEntityState() != EntityState.Original).ToList();

                foreach (Country c in countries)
                {
                    Save(c);
                }

                scope.Complete();
            }
        }

        public static void Save(Country country)
        {
            if (country.IsValid)
            {
                if (country.GetEntityState() == EntityState.New)
                {
                    CountriesRepository.Insert(country.GetRow());
                }
                else if (country.GetEntityState() == EntityState.Modified)
                {
                    CountriesRepository.Update(country.GetRow());
                }
            }
            else
            {
                throw new FaultException(string.Format(Errors.CanNotSaveCountryBecauseOf, country.Name, country.Error));
            }

        }

        public static List<Currency> GetAllCurrencies()
        {
            var rows = CurrenciesRepository.Select();
            var currencies = rows.ConvertAll<Currency>(c => new Currency(c));

            currencies.ForEach(c => c.ChangeState(EntityState.Original));

            return currencies;
        }

        public static Currency GetCurrencyById(int currencyId)
        {
            var row = CurrenciesRepository.Select(c => c.Id == currencyId).SingleOrDefault();

            var currency = new Currency(row);

            currency.ChangeState(EntityState.Original);

            return currency;
        }

        public static List<CurrencyExchangeRate> GetCurrencyExchangeRates(int countryId)
        {
            var rows = CurrencyExchangeRatesRepository.Select(c => c.CountryId == countryId);
            var rates = rows.ConvertAll<CurrencyExchangeRate>(row => new CurrencyExchangeRate(row));

            foreach (CurrencyExchangeRate rate in rates)
            {
                rate.SourceCurrency = GetCurrencyById(rate.SourceCurrencyId);
                rate.TargetCurrency = GetCurrencyById(rate.TargetCurrencyId);

                rate.ChangeState(EntityState.Original);
            }

            return rates;
        }

        public static void SaveCurrencyExchangeRates(List<CurrencyExchangeRate> rates)
        {
            try
            {
                using (TransactionScope scope = new TransactionScope())
                {
                    foreach (var rate in rates)
                    {
                        SaveCurrencyExchangeRate(rate);
                    }

                    scope.Complete();
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        private static void SaveCurrencyExchangeRate(CurrencyExchangeRate rate)
        {
            if (rate.IsValid)
            {
                if (rate.GetEntityState() == EntityState.New)
                {
                    CurrencyExchangeRatesRepository.Insert(rate.GetRow());
                }
                else if (rate.GetEntityState() == EntityState.Modified)
                {
                    CurrencyExchangeRatesRepository.Update(rate.GetRow());
                }
            }
            else
            {
                throw new FaultException(string.Format(Errors.CanNotSaveCurrencyExchangeRateBecauseOf, rate.Error));
            }

        }
    }
}
